<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="../lib/ext/resources/css/ext-all-neptune.css"/>
    <script type="text/javascript" src="../lib/ext/ext-all.js"></script>
    <script type="text/javascript" src="../lib/ckeditor/ckeditor.js"></script>
    <script type="text/javascript" src="../lib/ext_ux_ckeditor.js"></script>
    <script type="text/javascript" src="../lib/gf/common_grid.js"></script>
    <script type="text/javascript" src="../lib/ext_ux_treepicker.js"></script>

</head>
<script type="text/javascript">
    Ext.Loader.setConfig({enabled: true});
    var dataForm;
    var data = {};    

    Ext.onReady(function () {
        //保存数据
        function doAdd(){
            if(!dataForm.form.isValid()){
                Ext.Msg.alert('系统提示','请修正指出错误(红框)，检查输入是否正确!');
                return;
            }
            var params = dataForm.form.getFieldValues();
            var _url = '/article/add';
            if(params && params._id ){
                _url = '/article/edit';
            }
            if(params.begin_date){
                params.begin_date = Ext.util.Format.date(params.begin_date,'Ymd');
            }
            if(params.end_date){
                params.end_date = Ext.util.Format.date(params.end_date,'Ymd');
            }
            if(Ext.getCmp("type") && Ext.getCmp("type").getPicker() && Ext.getCmp("type").getPicker().selModel
                && Ext.getCmp("type").getPicker().selModel.getSelection() && Ext.getCmp("type").getPicker().selModel.getSelection().length > 0){
                params.type_path = Ext.getCmp("type").getPicker().selModel.getSelection()[0].getPath() + "/";
                params.type = Ext.getCmp("type").getPicker().selModel.getSelection()[0].getId();
                params.type_name = Ext.getCmp("type").getPicker().selModel.getSelection()[0].data.text;
                console.log("params.type_name: "+params.type_name);
            }            
            Ext.Ajax.request({
                url: _url,
                params : params,
                method : 'POST',
                success: function(response, opts) {
                    alert('业务处理成功');
                    // this.refresh();
                    // this.closeWin();
                },
                failure: function(response, opts) {
                    // alert('业务处理失败');
                    alert(Ext.JSON.decode(response.responseText).error);
                }
            });

        }
        //关闭窗口
        // function doClose(){
        //     this.closeWin();
        // }
        var tbar = [
            {pressed: true,text: '保  存',handler: doAdd,icon : '../img/book.png'}
            // {pressed: true,text: "关  闭",handler: doClose,icon : '../img/close.png'}
        ];
        dataForm = new Ext.FormPanel({
            autoScroll:true,
            overflowY: 'scroll',
            items: [{
                xtype: 'form',
                defaultType: 'textfield',
                layout: {
                    type: 'table',
                    columns: 3
                },
                defaults:{
                    labelAlign:'right'
                },
                items: [{
                        name: '_id',
                        hidden: true
                    }, {
                        name : "type_path",
                        hidden : true
                    }, {
                        name : "type_name",
                        hidden : true
                    },{
                        fieldLabel: '文章标题',
                        name: 'title',
                        colspan: 3,
                        width: 500,
                        allowBlank: false
                    },
                    {
                      xtype: 'treepicker',
                      id: 'type',
                      fieldLabel: '所属栏目',
                      name : 'type',
                      width: 500,
                      // labelWidth: 80,
                      // margin: '100 0 0 150',
                      //rootVisible:false,
                      displayField: 'text',
                      valueField: 'id',
                      minPickerHeight: 200,//默认最小高度
                      cascade : 'child',//级联方式:1.child子级联(仅向下勾选子节点)；2.parent父级联(仅向上勾选父节点)；3.both全部级联(向上向下的父/子节点均会勾选)  
                      checkModel:'single',//当json数据为不带checked的数据时只配置为single,带checked配置为double为单选,不配置为多选 
                      store: Ext.create('Ext.data.TreeStore',{
                        root: {
                          text: '全部',
                          expanded: true
                        },
                        proxy: {
                          type: 'ajax',
                          url: '/dict/tree_ext?parent_code=article_category',
                          // url: '/dict/tree_ext?parent_code=root',
                          reader: {
                            type: 'json'
                          }
                        }
                      })
                    }, {
                        fieldLabel: '显示顺序',
                        name: 'sorder',
                        xtype: 'numberfield',
                        colspan:2,
                        value: 0
                    }, {
                        fieldLabel: '文章作者',
                        name: 'author'
                    }, {
                        name: 'source_from',
                        fieldLabel: '文章来源'
                    },{
                        xtype : "label"
                    }, {
                        fieldLabel: '来源日期',
                        name: 'source_date',
                        xtype: 'datefield',
                        editable: false,
                        format: 'Y-m-d',
                        value: new Date()
                    }, {
                        fieldLabel: '来源网站',
                        colspan: 2,
                        name: 'source_web'
                    }, {
                        name: 'source_url',
                        fieldLabel: '来源网址'
                    }, {
                        fieldLabel: '文章编码',
                        colspan: 2,
                        name: 'code'
                    },
                    {
                        xtype: "filefield",
                        fieldLabel: '封面图片上传',
                        name: 'file_pt_ctl',
                        width: 600
                    }, {
                        xtype: "button",
                        text: "上传封面图片",
                        handler: function() {
                            uploadFileCb(dataForm, "", "file_pt_ctl", function(err, result) {
                                if (err) {
                                    alert(err);
                                    return;
                                }
                                dataForm.getForm().findField("image_page_title_id").setValue(result.file_id);
                            })
                        },
                        style: 'margin-left: -100px;'
                    }, {
                        xtype: "textfield",
                        fieldLabel: '封面图片id',
                        name: 'image_page_title_id',
                        readOnly: true
                    }, {
                        xtype : "textarea",
                        fieldLabel: "摘要",
                        name : "summary",
                        colspan : 3,
                        width : 800
                    },{
                        xtype: 'ckeditor',
                        name: 'content',
                        height: 800,
                        colspan: 3,
                        width: "100%",
                        CKConfig: {
                            width: '100%',
                            height: 800,
                        }
                    }
                ],
                tbar: tbar
            }]
        });
        var viewport = new Ext.Viewport({
            layout: 'fit',
            // autoSroll : true,
            renderTo: Ext.getBody(),
            items: [dataForm]
        });
        //获取url中的参数，如果传入了参数_id那么进行修改；否则，添加。
        var _urlparams = Ext.urlDecode(document.URL.split("?")[document.URL.split("?").length - 1]);
        if(_urlparams && _urlparams._id){
            Ext.Ajax.request({
                url: '/article/detail',
                params : {'_id' : _urlparams._id},
                method : 'get',
                success: function(response, opts) {
                    data =  Ext.decode(response.responseText);
                    console.log(data);
                    init_form();
                },
                failure: function(response, opts) {
                    alert('加载失败......');
                    init_form();
                }
            });
        }

        function init_form(){
            if(!data){
                return ;
            }

            for(var i in data){
                if(dataForm.form.findField(i)){
                    dataForm.form.findField(i).setValue(data[i]);
                }
            }
            if(data.begin_date){
                dataForm.form.findField('begin_date').setValue(Ext.Date.parse(data.begin_date,'Ymd'));
            }
            if(data.end_date){
                dataForm.form.findField('end_date').setValue(Ext.Date.parse(data.end_date,'Ymd'));
            }
            //dataForm.form.findField('content').setValue(data.content);
            var _id = document.getElementsByName('content').item(0).id;
            CKEDITOR.instances[_id].setData(data.content);
        }
    });
</script>
<body>
</body>
</html>